home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 719 < prev    next >
Internet Message Format  |  1996-08-06  |  4KB

  1. Path: inforamp.net!usenet
  2. From: pcurran@inforamp.net (Peter Curran)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Restrictions on qsort compare function?
  5. Date: Wed, 10 Apr 1996 01:52:07 GMT
  6. Organization: PSC Enterprises
  7. Message-ID: <4kf46j$b7a@sam.inforamp.net>
  8. References: <4kbl1l$74r@sam.inforamp.net> <829068682snz@genesis.demon.co.uk>
  9. Reply-To: pcurran@inforamp.net
  10. NNTP-Posting-Host: ts47-13.tor.istar.ca
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. Well, I said I was stopping, but . . .
  14.  
  15. On Tue, 09 Apr 96 16:51:22 GMT in article <829068682snz@genesis.demon.co.uk>
  16.     Lawrence Kirby <fred@genesis.demon.co.uk> (Lawrence Kirby) wrote:
  17.  
  18. <snip>
  19.  
  20. >Or use rand()! However the relation is defined purely on the values of the
  21. >keys. The standard embodies this idea too -
  22.  
  23. >"The function shall return an integer less than, equal to, or greater than
  24. > zero if the first argument is considered to be respectively less than, equal
  25. > to, or greater than the second"
  26.  
  27. >gives no license for the comparison function to consider anything other than
  28. >its arguments in determining the return value. I would certainly accept that
  29. >this is worded badly i.e. "first argument" should read "object pointed to by
  30. >the first argument" and similarly for "second". However this loose usage
  31. >appears elsewhere in the standard and I believe the intent is clear (I'm
  32. >happy to discuss that further).
  33.  
  34. This implies that looking at a (global) data block to determine, say, what the
  35. current collating sequence is, or whether the user has requested "forward" or
  36. "reverse" ordering, is invalid.  Such a suggestion would certainly contradict my
  37. idea of what constitues a reasonable comparison function.  
  38.  
  39. <snip>
  40.  
  41. >My argument is based fundamentally on 3.16:
  42.  
  43. >"Undefined behaviour is otherwise indicated in this International Standard
  44. > by the words ``undefined behaviour'' or by the omission of any explicit
  45. > defintion of behaviour. There is no difference in emphasis among these
  46. > three; they all describe ``behaviour that is undefined.''"
  47.  
  48. >This means that code has undefined behaviour unless you can show what the
  49. >defined behaviour is according to the standard. In all of your examples
  50. >the behaviour can vary depending on the actual implementation of qsort().
  51. >In the absense of any other limitation in the standard (such as a statement
  52. >of implementation-defined behaviour), this means the behaviour is undefined.
  53.  
  54. >To disprove my assertion for any of your examples the bottom line is to
  55. >show, by reference to the standard, what the defined behaviour is. An
  56. >example is putting forward a reasonable definition of 'sort' which would
  57. >make the example well defined.
  58.  
  59. I have described comparison functions that return values that appropriately
  60. reflect what I said I consider to be the relative order of the values passed to
  61. them.  This is what the standard says they must do, and they do it.  I do not
  62. see any reason to consider undefined behaviour to have been invoked.  (I do see
  63. it possible when the values of the (pointer) parameters are taken into account
  64. in the function, as I explained earlier, but I am not convinced that the
  65. comparison function can be held responsible, based on the standard.)
  66.  
  67. >Something else to consider is that the bsearch() function defines a
  68. >comparison function in essentially the same way as the qsort() function
  69. >does (except that it refers correctly to the key object and array element
  70. >rather than just 'arguments'), so a valid qsort() comparison function
  71. >ought to be a valig bsearch() comparison function.
  72.  
  73. Agreed - the details of value/pointer-to-value text in the standard should be
  74. corrected, but it is not germane to the discussion here - I am assuming the
  75. obvious intent.
  76.  
  77. --
  78. Peter Curran                               pcurran@inforamp.net
  79.  
  80.